<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText" :titleExplain="titleExplain"></topTitle>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="所属地区" class="row-padding-bottom">
              <el-cascader
                ref="manageArea"
                v-model="areaId"
                :options="areasOptions"
                @change="getAreaId"
                clearable
                filterable
                :props="{ checkStrictly: true }"
                :append-to-body="false"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="行业" class="row-padding-bottom">
              <el-select
                v-model="filterField.industryId"
                filterable
                placeholder="请选择"
                clearable
                :popper-append-to-body="false"
              >
                <el-option
                  v-for="item in industryOptions"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="建档日期" class="row-padding-bottom">
                <timeSelection
                  @getStartTimeValue="getStartTimeValue"
                  @getEndTimeValue="getEndTimeValue"
                ></timeSelection>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="开工状态" class="row-padding-bottom">
                <el-select
                  v-model="filterField.status"
                  filterable
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                    :popper-append-to-body="false"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="建设方" class="row-padding-bottom">
                <el-input
                  v-model="filterField.pcName"
                  clearable
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="施工方" class="row-padding-bottom">
                <el-input
                  v-model="filterField.pbName"
                  clearable
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="项目名称" class="row-padding-bottom">
                <el-input
                  v-model="filterField.projectName"
                  placeholder="请输入项目名称"
                  clearable
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="评价等级" class="row-padding-bottom">
                <el-select
                  v-model="filterField.gradeLevel"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <!-- <el-option v-for="item in levelOptions" :key="item.code" :label="item.name" :value="item.code"></el-option> -->
                  <el-option
                    v-for="(item, index) in selectLevel"
                    :key="index"
                    :label="item.name"
                    :value="item.gradeLevel"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="红黑榜" class="row-padding-bottom">
                <el-select
                  v-model="filterField.rdStatus"
                  filterable
                  placeholder="请选择"
                  no-data-text="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option label="红榜" value="1"></el-option>
                  <el-option label="黑榜" value="0"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item label="">
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
              <!-- <exportBtn :dataLen="tableList.length" :filterField="filterField" :api="api"></exportBtn> -->
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <div style="margin-bottom: 24px">
          <exportBtn
            :dataLen="tableList.length"
            :filterField="filterField"
            responseType="get"
            api="api-ps/creditProjectScore/expand/export"
          ></exportBtn>
          <el-button
            class="commonBut saveBtn funComBut outEmployee"
            v-sdc
            @click="setRedBlack('red')"
          >
            <span class="opetareText">设置红榜</span>
          </el-button>
          <el-button
            class="commonBut saveBtn funComBut outEmployee"
            v-sdc
            @click="setRedBlack('black')"
          >
            <span class="opetareText">设置黑榜</span>
          </el-button>
          <el-button
            class="commonBut saveBtn funComBut outEmployee"
            v-sdc
            @click="toClear"
          >
            <span class="opetareText">清除标记</span>
          </el-button>
        </div>
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="tableList"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          style="border: 1px solid #dfe6ec"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="80"
            align="center"
            fixed="left"
          ></el-table-column>
          <el-table-column
            prop="projectName"
            label="项目名称"
            width="230"
            fixed="left"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="areaDetailDesc"
            label="地区"
            :formatter="formatTd"
            width="150"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.areaDetailDesc"
                :columnName="'areaDetailDesc'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column prop="pcName" label="建设方" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.pcName"
                :columnName="'pcName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column prop="pbName" label="施工方" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.pbName"
                :columnName="'pbName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="score"
            label="评价"
            align="center"
            :formatter="formatTd"
            min-width="140"
          >
            <template slot-scope="scope">
              <span
                >{{ scope.row.score }} /
                {{
                  scope.row.gradeLevel === 'O' ? '其他' : scope.row.gradeLevel
                }}</span
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="projectCreateTime"
            label="建档日期"
            align="center"
            :formatter="formatTd"
            min-width="130"
          ></el-table-column>
          <el-table-column
            prop="statusDesc"
            label="项目状态"
            align="center"
            :formatter="formatTd"
            min-width="120"
          ></el-table-column>
          <el-table-column
            prop="rdStatus"
            label="红黑榜"
            align="center"
            min-width="120"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                class="smallBtn redBtn"
                v-if="scope.row.rdStatus == '1'"
                >红榜</el-button
              >
              <el-button
                size="mini"
                class="smallBtn blackBtn"
                v-else-if="scope.row.rdStatus == '0'"
                >黑榜</el-button
              >
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'
import scrollTable from '@/mixin/scrollTable.js'

export default {
  name: 'patrolTemplate',
  mixins: [scrollTable],
  data() {
    return {
      titleText: '项目红黑榜设置',
      advanced: false,
      titleExplain: '',
      levelOptions: [], //评价等级
      statusOptions: [], //状态
      //表格数据
      tableList: [],
      requestUrl:
        'api-ps/creditProjectScore/expand/getProjectScoreListByMonitor',
      areaId: '', //区域
      //搜索条件
      filterField: {
        sysAppCode: localStorage.getItem('judgeRoleCode'),
        projectId: '',
        projectName: '', //项目名称
        areaId: '', //区域
        status: '', //开工状态
        industryId: '', //行业
        gradeLevel: '', //评价等级
        pcName: '', //建设方
        pbName: '', //施工方
        startDate: '',
        endDate: '',
        serverArea: '', // 服务部署区域
        rdStatus: '', //是否是红黑榜
      },
      isShow: false, //列表加载
      loading: true,
      loginType: '',
      filingDate: [],
      selectLevel: [],
      isShowDetail: '',
      tableDateSelcted: [],
    }
  },
  computed: {
    ...mapState(['areasOptions', 'industryOptions']),
  },
  created() {
    // this.getAreas({areaId: this.$store.state.areaInfoArr.areaId});

    // 开工状态
    this.getStatus()
    this.getLevelList('20') //项目评级配置
    //获取行业
    //    this.getIndustry({});
    // this.getProject();
    this.filterField.serverArea = this.$store.state.areaInfoArr.areaId
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    ...mapActions(['getAreas', 'getIndustry']),
    /**
     * @description: 获取开始日期
     * @param {*} urls
     * @param {*} datas
     */
    getStartTimeValue(val) {
      if (val) {
        this.filterField.startDate = val
      } else {
        this.filterField.startDate = ''
      }
    },
    /**
     * @description: 获取结束日期
     * @param {*} urls
     * @param {*} datas
     */
    getEndTimeValue(val) {
      if (val) {
        this.filterField.endDate = val
      } else {
        this.filterField.endDate = ''
      }
    },
    // 获取评级配置
    getLevelList(type) {
      return this.axios({
        method: 'post',
        url: 'api-ps/creditLevel/expand/getCreditLevelList',
        data: {
          type,
        },
      })
        .then((response) => {
          if (!response.data.hasError) {
            const results = response.data.result
            results.forEach((v) => {
              v.name = v.gradeLevel
            })
            this.selectLevel = results
            const otherLevel = { gradeLevel: 'O', name: '其他' }
            this.selectLevel.push(otherLevel)
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    // 获取评价等级和开工状态
    getStatus() {
      this.axios({
        method: 'post',
        url: '/api-ps/creditProjectScore/expand/findprowashbytype',
        data: { type: 'PROJECT_STATUS' },
      })
        .then((res) => {
          if (!res.data.hasError) {
            this.statusOptions = res.data.result.filter(
              (v) => v.code != 'NOT_STARTED'
            )
          } else {
            this.$message({
              showClose: true,
              duration: 3000,
              message: res.data.errorMsg,
              type: 'error',
            })
          }
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 3000,
            message: '请求失败',
            type: 'error',
          })
        })
    },
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    handleSelectionChange(val) {
      this.tableDateSelcted = val
    },
    // 设置红黑榜
    setRedBlack(type) {
      if (this.tableDateSelcted.length < 1) {
        this.$message.warning('请先选择项目')
        return
      }
      // 设置红榜
      var status = ''
      if (type == 'red') {
        status = '1'
      } else if (type == 'black') {
        // 设置黑榜
        status = '0'
      } else {
        // 设置信用免单  isFree：1
      }
      var addArr = []
      this.tableDateSelcted.forEach((item, i) => {
        let obj = {}
        obj.id = item.rdId
        obj.type = 'PROJECT'
        obj.projectId = item.projectId
        obj.status = status
        // obj.uscc = item.uscc;
        obj.companyType = item.type
        addArr.push(obj)
      })

      var data = {
        type: 'PROJECT',
        redBlacks: JSON.stringify(addArr),
      }
      this.axios({
        method: 'post',
        url: 'api-ps/creditredblack/setRedBlack',
        data: data,
      })
        .then((response) => {
          if (response.data.hasError) {
            this.$message.error(response.data.errorMsg)
          } else {
            this.$message.success(response.data.msg || '操作成功')
            this.$nextTick(function () {
              this.$refs.pagination.getTableListData()
            })
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    //清除标记
    toClear() {
      if (this.tableDateSelcted.length < 1) {
        this.$message.warning('请先选择企业')
        return
      }
      var arr = []
      this.tableDateSelcted.forEach((item) => {
        arr.push(item.rdId)
      })
      this.axios({
        method: 'post',
        url: 'api-ps/creditredblack/remove',
        data: {
          ids: JSON.stringify(arr),
        },
      })
        .then((response) => {
          if (response.data.hasError) {
            this.$message.error(response.data.errorMsg)
          } else {
            this.$message.success(response.data.msg || '移除成功')
            this.$nextTick(function () {
              this.$refs.pagination.getTableListData()
            })
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
  },
}
</script>

<style scoped lang="less">
@import '../../../static/css/filterArea.less';
.smallBtn {
  color: #fff;
  border: none;
  cursor: default;
}
.redBtn {
  background-color: #d71717;
}

.blackBtn {
  background-color: #000;
}
</style>
